package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.payStandard.PayStandard;

public class PayStandardServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
			this.doPost(request, response);	
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String m = request.getParameter("m");

		if ("list".equals(m)) {
			process(request, response, "/page/payStandard/payStandard_list.jsp");
		} else if ("open".equals(m)) {
			process(request, response, "/page/payStandard/payStandard_add.jsp");
		}else if("add".equals(m)){
			add(request,response);
			process(request,response,"/page/payRecord/payRecord_list.jsp");
		}
	}
	
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		// 1:接收表单传过来的值
		String thisYear = req.getParameter("annual");
		String startTimeStr = req.getParameter("startTime");
		String endTimeStr = req.getParameter("endTime");
		try {
			double account = Double.parseDouble(req.getParameter("account"));
			// 将接收的时间转换成Date类型
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date startTime = sdf.parse(startTimeStr);
			Date endTime = sdf.parse(endTimeStr);
			
			String registrar=(String)req.getSession().getAttribute("userName");
			// 如果当前年度已存在则执行更新操作，否则执行添加操作
			PayStandard model = new PayStandard(thisYear);
			if(model.getAccount() == 0 || "".equals(model.getAccount())) {

				PayStandard psd = new PayStandard(thisYear, account, startTime, endTime, registrar);

				psd.AddPayStandard();
			}else {

				PayStandard psd = new PayStandard(thisYear, account, startTime, endTime, registrar);

				psd.UpdatePayStandard();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

}
